<?
/**
* This is where all information of user stored
* @author Adesanto Asman
* @version 1.0
* @copyright Copyright (c)2007, Adesanto Asman
* @package UID
*/

/**
* All vars of classUID are define here
* @package UIDVars
*/
class UIDVars{

  /**
  * @var string name
  */
	var	$name;

	/**
  * @var string ID
  */
	var $UID;

	/**
  * @var string Position
  */
	var $pos;

	/**
  * @var string CodeDepartment
  */
	var $dept;

	/**
  * @var string NameDepartment
  */
	var	$deptNm;

	/**
  * @var string Group of Position
  */
	var $grpPos;

	/**
  * @var array List of moduls
  */
	var $uMdl		= array();

	/**
  * @var array List of Privileges
  */
	var	$privs	= array("STATUS",				    //
											"ENTRY_RIGHTS",			//
											"EDIT_RIGHTS",			//
											"DELETE_RIGHTS",		//
											"VIEW_RIGHTS",			// ALL of these are preinitialized privs... non dynamic
											"FINALIZE_RIGHTS",	//
											"APPROVED_RIGHTS",	//
											"JOURNAL_RIGHTS",		//
											"PROGRAMS",					//
											"PICT");						//
	/**
  * @var boolean
  */
	var $status;
}

/**
* Extends UIDVars
* @package UIDMth
* @see UIDVars
*/
class UIDMth extends UIDVars{

	/**
	 * Here where you are init the list of category and group of category,
	 * Also this method raise a flag when it encounters any error
	 * @see UIDVars, $status
	 * @param string $ORCLU Name of ORACLE DB Scheme
	 * @param string $OPASS PWD of ORACLE DB Scheme
	 * @param string $OSID Service name of ORACLE DB Scheme
	 * @param string $UID the user' ID
	 * @param string $compSel the user' Company
	 */
	function UIDMth( $UID, $compSel, $ORCLU, $OPASS, $OSID){

		if ($conn=OCILogon( $ORCLU, $OPASS, $OSID)) {

			$srcDbUsr		= "USERID_COMPANY";
			$srcDbUNm		= "USERID_HDR";
			$srcDbModul	= "USERID_DTL";
			$srcDbPos		= "GROUP_POS";
			$srcDbDept	=	"MST_DEPT";

			$query	=	"select USERNAME, ".
			                  "POS, ".
			                  "DEPARTMENT ".
			          "FROM ".$srcDbUNm." ".
			          "WHERE USERID='".$UID."' ";	//fetch username
			$s	=	OCIParse($conn, $query);
			OCIExecute($s, OCI_DEFAULT);
			$nrowsNm	= oci_fetch_all($s, $resNm);
			if($nrowsNm > 0){

				$query	=	"select DESCRIPTION AS DESCP, ".
				                  "LEVELMN, ".
				                  "STATUS, ".
				                  "ENTRY_RIGHTS, ".
				                  "EDIT_RIGHTS, ".
				                  "DELETE_RIGHTS, ".
				                  "VIEW_RIGHTS, ".
				                  "FINALIZE_RIGHTS, ".
									        "APPROVED_RIGHTS, ".
									        "JOURNAL_RIGHTS, ".
									        "PROGRAMS, ".
									        "PICT ".
									"FROM ".$srcDbModul." ".
									"WHERE  USERID='".$UID."' ".
									        "and COMPANY='".$compSel."' ".
									        "AND STATUS=1 ". //STATUS = 1 MEANS ACTIVE
									"ORDER BY LEVELMN ASC";
	      //echo "(".__FILE__.":".__LINE__.") $query <br/>\n";
				$s	=	OCIParse($conn, $query);
				OCIExecute($s, OCI_DEFAULT);
				$nrowsPrivs	= oci_fetch_all($s, $resPrivs);
				if($nrowsPrivs > 0){

					$this->name		=	$resNm["USERNAME"][0];
					$this->UID		=	$UID;
					$this->pos		=	$resNm["POS"][0];
					$this->dept		=	$resNm["DEPARTMENT"][0];
					$this->deptNm	= $resDeptNm["DEPT_NAME"][0];
					$this->grpPos	=	($resGrpPos["GROUP_POS"][0] == ' ' || $resGrpPos["GROUP_POS"][0] == '')? "NO GROUP" : $resGrpPos["GROUP_POS"][0];
					$this->uMdl		= $resPrivs["DESCP"];

					$this->privs["LEVELMN"]					=	$resPrivs["LEVELMN"];
					$this->privs["STATUS"]			    =	$resPrivs["STATUS"];
					$this->privs["ENTRY_RIGHTS"]		=	$resPrivs["ENTRY_RIGHTS"];
					$this->privs["EDIT_RIGHTS"]			=	$resPrivs["EDIT_RIGHTS"];
					$this->privs["DELETE_RIGHTS"]		=	$resPrivs["DELETE_RIGHTS"];
					$this->privs["VIEW_RIGHTS"]			=	$resPrivs["VIEW_RIGHTS"];
					$this->privs["FINALIZE_RIGHTS"]	=	$resPrivs["FINALIZE_RIGHTS"];
					$this->privs["APPROVED_RIGHTS"]	=	$resPrivs["APPROVED_RIGHTS"];
					$this->privs["JOURNAL_RIGHTS"]	=	$resPrivs["JOURNAL_RIGHTS"];
					$this->privs["PROGRAMS"]				=	$resPrivs["PROGRAMS"];
					$this->privs["PICT"]						=	$resPrivs["PICT"];

					OCILogoff($conn);
					$this->status = FLAG_GO;
				}else{
				  echo  "User' Modul initialisation failed!<br/>\n";
					trigger_error("User' Modul initialisation failed!", E_ERROR_ERROR);
					$this->status = FLAG_NOGO;
				}
			}else{
			  echo  "USERID doesn't exist!<br/>\n";
				trigger_error("USERID doesn't exist!", E_USER_ERROR);
				$this->status = FLAG_NOGO;
			}
		}else{
		  echo  "Oracle connection Failed!<br/>\n";
		  trigger_error("Oracle connection Failed!", E_USER_ERROR);
			$this->status = FLAG_NOGO;
		}
	}

	/**
	 * Checks if the user' position is none of top-defined then the query needs addition condition clauses
	 * @param void
	 * @return array
	 */
	function getAddition(){

		$uPos	= $this->pos;
		if($uPos == "GM"    ||

		   $uPos == "VGM"   ||
		   $uPos == "AGM"   ||
		   $uPos == "MDV"   ||
		   $uPos == "WMDV"  ||
		   $uPos == "FIN"   ||
		   $uPos == "PLG"   ||
		   $uPos == "SUBS"){
			 $retBuff = ""; //empty no addition
		}else{

			$retBuff = " DEPT_CODE='".$this->dept."' ";
		}

		return $retBuff;
	}

	/**
	 * Return user' name
	 * @see UIDVars, $name
	 * @param void
	 * @return string
	 */
	function getName(){

		return $this->name;
	}

	/**
	 * Return user' ID
	 * @see UIDVars, $UID
	 * @param void
	 * @return string
	 */
	function getUID(){

		return $this->UID;
	}

	/**
	 * Return user' Position
	 * @see UIDVars, $pos
	 * @param void
	 * @return string
	 */
	function getPos(){

		return $this->pos;
	}

	/**
	 * Return user' IdDepartment
	 * @see UIDVars, $dept
	 * @param void
	 * @return string
	 */
	function getDept(){

		return $this->dept;
	}

	/**
	 * Return user' NameDepartment
	 * @see UIDVars, $deptNm
	 * @param void
	 * @return string
	 */
	function getDeptNm(){

		return $this->deptNm;
	}

	/**
	 * Return user' Group of Position
	 * @see UIDVars, $grpPos
	 * @param void
	 * @return string
	 */
	function getGrpPos(){

		return $this->grpPos;
	}

	/**
	 * Return user' Privilege of specified modul
	 * @see UIDVars, $uMdl
	 * @param string $modul the name of modul
	 * @param string $priv  the type of privilege
	 * @return string
	 */
	function getPrivs($modul,$priv){

		$idx  = array_search($modul, $this->uMdl);
		if(strlen($idx) > 0){

		  return $this->privs[$priv][$idx];
		}else{

		  return 0;
		}
	}

  /**
   * Return user' Privilege of specified modul number
   * @see UIDVars, $uMdl
   * @param string $modul the name of modul
   * @param string $priv  the type of privilege
   * @return string
   */
  function getPrivsByNumber($modulNumber,$priv){

    $idx  = array_search($modulNumber, $this->privs["LEVELMN"]);
    if(strlen($idx) > 0){

      return $this->privs[$priv][$idx];
    }else{

      return 0;
    }
  }

	/**
	 * Return the status of operation that needs to be check
	 * @see UIDVars, $status
	 * @param void
	 * @return boolean
	 */
	function getStatus(){

	  return $this->status;
	}

	/**
	 * Init user dept code manually
	 * @see $dept
	 * @param string
	 */
	function initDeptManually($dept){

	  $this->dept = $dept;
	}

	/**
	 * Checks if the user' position is none of top-defined then return IS_DEPT
	 * @param void
	 * @return boolean
	 */
	function isDept(){

		$uPos	= $this->pos;

		if($uPos == "GM"    ||
		   $uPos == "VGM"   ||
		   $uPos == "AGM"   ||
		   $uPos == "MDV"   ||
		   $uPos == "WMDV"  ||
		   $uPos == "FIN"   ||
		   $uPos == "PLG"   ||
		   $uPos == "SUBS"){

			return FALSE;
		}else{

			return TRUE;
		}
	}
}

define('FLAG_GO'    , "GO");
define('FLAG_NOGO'  , "NO_GO");

?>